﻿/*****************************************************************************
* File			: src.LevelManager
* Purpose		: Contains Declaration and Implementation for LevelManager class
* Creation date : April 26, 2009
* Author		: Sami TOUIL
******************************************************************************/
package src
{
	// Imports
	import flash.utils.Timer
    import flash.events.*
    import flash.display.Sprite
	
	/**************************************
	* Class		: LevelManager
	* Purpose	: Logic for level behavior
	* Parent	: MovieClip
	**************************************/
	public class LevelManager
	{
		// Class variables
		
		public static	var main;						// Refer to the current and only level manager
	
		public 			var nMiniBossCount	:int;		// Number of minibosses before final boss
	
		public 			var nBossInterval	 :int;		// Interval before final boss pops
		public 			var nMiniBossInterval:int;		// Interval between miniboss pops
		public 			var nPowerupInterval :int;		// Interval between powerups pops
		
		public 			var	bossTimer		:Timer;		// Timer to manage Boss pops
		public 			var miniBossTimer	:Timer;		// Timer to manage miniBoss pops
		public 			var enemyTimer		:Timer;		// Timer to manage enemies pops
		public 			var powerupTimer	:Timer;		// Timer to manage powerup pops
		public 			var winTimer		:Timer;		// Timer to manage a game win !
		
		// constructor
		public function LevelManager()
		{
			main = this;
			nBossInterval		= 5;    // Time before final boss pops
			nMiniBossInterval 	= 30;	// Seconds between minibosses
			nPowerupInterval 	= 10;	// Seconds between powerups
			initLevel();
		}
		
		/***********************************************************
		* Method		: init()
		* Purpose		: Initialize a new Level manager or reset the existing one
		* Parameters	: None
		* return		: void
		***********************************************************/
		static public function init()
		{
			if(main == null)
				new LevelManager();
			else
				main.reset();
		}
		
		/***********************************************************
		* Method		: initLevel()
		* Purpose		: Initialize a new level
		* Parameters	: None
		* return		: void
		***********************************************************/
		public function initLevel()
		{
			// Init Number of minibosses before final boss
			nMiniBossCount = 3;	
			
			// Start scrolling
			Background.startScrolling();
			
			//init ship release timer, release enemy ship once a second
			enemyTimer = new Timer(1000, 0);
			enemyTimer.addEventListener("timer", enemyTimerHandler);
			enemyTimer.start();
			
			//init boss release timer but do no start it
			bossTimer = new Timer(1000 * nBossInterval, 1);
			bossTimer.addEventListener("timer", bossTimerHandler);
			
			//init miniboss release timer
			miniBossTimer = new Timer(1000 * nMiniBossInterval, 0);
			miniBossTimer.addEventListener("timer", miniBossTimerHandler);
			miniBossTimer.start();	
			
			//init powerup release timer
			powerupTimer = new Timer(1000 * nPowerupInterval, 0);
			powerupTimer.addEventListener("timer", powerupHandler);
			powerupTimer.start();
		}
		
		/***********************************************************
		* Method		: enemyTimerHandler()
		* Purpose		: Pop a new enemy
		* Parameters	: None
		* return		: void
		***********************************************************/
		public function enemyTimerHandler(event:TimerEvent):void 
		{
        	new Enemy();
        }

		/***********************************************************
		* Method		: bossTimerHandler()
		* Purpose		: Pop a new miniboss
		* Parameters	: None
		* return		: void
		***********************************************************/
		public function bossTimerHandler(event:TimerEvent):void 
		{
			new Boss();
			bossTimer.stop();
        }

		/***********************************************************
		* Method		: miniBossTimerHandler()
		* Purpose		: Pop a new miniboss
		* Parameters	: None
		* return		: void
		***********************************************************/
		public function miniBossTimerHandler(event:TimerEvent):void 
		{
			if (nMiniBossCount >0)
			{
        		new MiniBoss();			// Create a new miniboss and decrease count if the counter is not null
				nMiniBossCount -= 1;
			}
			else
			{
				bossTimer.start();		// Else start timer for the final boss
				miniBossTimer.stop();	// and stop the miniboss timer
			}
        }
		
		/***********************************************************
		* Method		: powerupHandler()
		* Purpose		: Pop a new powerup
		* Parameters	: None
		* return		: void
		***********************************************************/
		public function powerupHandler(event:TimerEvent):void 
		{
        	new PowerUp();
        }
		
		/***********************************************************
		* Method		: stop()
		* Purpose		: Stop level by stopping all Timers
		* Parameters	: None
		* return		: void
		***********************************************************/
		public function stop()
		{
			// Stop scrolling
			Background.stopScrolling();
			
			// Stop timers
			enemyTimer.stop();
			bossTimer.stop();
			miniBossTimer.stop();
			powerupTimer.stop();
			
		}
		
		/***********************************************************
		* Method		: reset()
		* Purpose		: reset level manager by restarting timers
		* Parameters	: None
		* return		: void
		***********************************************************/
		public function reset()
		{
			// Stop all timers
			stop();
			
			// And then remove all listeners
			enemyTimer.removeEventListener("timer", enemyTimerHandler);
			bossTimer.removeEventListener("timer", bossTimerHandler);
			miniBossTimer.removeEventListener("timer", miniBossTimerHandler);
			powerupTimer.removeEventListener("timer", powerupHandler);
			
			// Initialize everything again
			initLevel();
		}
		
		/***********************************************************
		* Method		: win()
		* Purpose		: Game winned by destroying the boss
		* Parameters	: None
		* return		: void
		***********************************************************/
		public function win()
		{
			// Stop level manager
			stop();
			
			// Enter ship final move
			if (Game.main.ship != null)
				Game.main.ship.bFinalMove = true;
			
			//create a 2 second delay timer before showing the game over screen
			winTimer = new Timer(2000, 1);
			winTimer.addEventListener("timer", winHandler);
			winTimer.start();
		}
		
		/***********************************************************
		* Method		: winHandler()
		* Purpose		: timer to display Win menu after a short time
		* Parameters	: None
		* return		: void
		***********************************************************/
		public function winHandler(event:TimerEvent)
		{
			//remove this timer
			winTimer.removeEventListener("timer", winHandler);
			
			// Stop scrolling
			Background.stopScrolling();
			
			// Show gamewin menu
			Game.main.gameWinMenu.visible = true;
			
			// add listeners for PlayAgain button
			Game.main.gameWinMenu.playAgainButton.addEventListener(MouseEvent.CLICK, Game.main.onPlayAgain);
		}
	}
}